<script setup lang="ts">
//
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import hotPanel from './components/HotPanel.vue'
import Skeleton from './components/Skeleton.vue'
import { getHomeBannerAPI,getHomeCategoryAPI,getHomeHotAPI } from '@/services/home'

import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, HotItem } from '@/types/home'
import type { CategoryItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'

//子组件实例
const guessRef = ref<XtxGuessInstance>()

let bannerList = ref<BannerItem[]>([])
//获取banner数据
const getHomebannerData = async () =>{
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

//获取分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () =>{
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result

}

//获取热门数据
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () =>{
  const res = await getHomeHotAPI()
  hotList.value = res.result

}
//调用子组件方法，获取猜你喜欢数据
const getScrollData = () =>{
  guessRef.value?.getMore()
}

//是否开启下拉刷新动画
const isTriggered = ref(false)
// 下拉刷新
const onRefresherrefresh = async () =>{
  isTriggered.value = true
  //重置猜你喜欢数据
  guessRef.value?.resetData()
  // 调用异步方法
 await Promise.all([
    getHomebannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore()
  ])
  //关闭下拉刷新动画
  isTriggered.value = false
}

const isLoading = ref(false)

onLoad( async () => {
  isLoading.value = true
  await Promise.all([getHomebannerData(),getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})

</script>

<template>
  <!-- // 导航栏 -->
  <custom-navbar  />
  <Skeleton v-if="isLoading" />
  <scroll-view
      v-else
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      @scrolltolower="getScrollData"
      :refresher-triggered="isTriggered"
      class="scroll-view"
      scroll-y>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList"/>
    <!-- 分类 -->
      <CategoryPanel :list="categoryList" />
    <!-- 热门推荐 -->
    <hotPanel :list="hotList" />
    <!-- 猜你喜欢 -->
      <XtxGuess ref="guessRef"/>
  </scroll-view>


</template>

<style lang="scss">
page{
  display:flex;
  flex-direction:column;
  height:100%;
  background-color: #f7f7f7;
}
.scroll-view{
  flex:1;

}
</style>
